package demo.spark.local;

import java.util.Arrays;
import java.util.Iterator;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.api.java.function.Function2;
import org.apache.spark.api.java.function.VoidFunction;

public class SparkLocalDemo {

    static String filePath = "/Users/xmly/Downloads/logIdentity.csv";

    public static void main(String[] args) {
        String filePath = "/Users/xmly/Downloads/logIdentity.csv";
        filePath = "‪‪pom.xml";
        SparkConf sparkConf = new SparkConf();
        sparkConf.setAppName("sparkLocalDemo");
        sparkConf.setMaster("local");

        JavaSparkContext context = new JavaSparkContext(sparkConf);
        JavaRDD<String> lines = context.textFile(filePath);
        JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public Iterator<String> call(String t) throws Exception {
                return Arrays.asList(t.split(",")).iterator();
            }
        });
        words.reduce(new Function2<String, String, String>() {

            @Override
            public String call(String v1, String v2) throws Exception {
                // TODO Auto-generated method stub
                return null;
            }
        });
        System.out.println("打印");
        // filter 过滤
        JavaRDD<String> filtered = words.filter(new Function<String, Boolean>() {
            @Override
            public Boolean call(String v1) throws Exception {
                return "db".equals(v1);
            }
        });
        filtered = filtered.map(new Function<String, String>() {

            @Override
            public String call(String v1) throws Exception {
                return v1 + "#######";
            }
        });
        long count = filtered.count();
        System.out.println("count = " + count);
        filtered.foreach(new VoidFunction<String>() {
            @Override
            public void call(String t) throws Exception {
                System.out.println(t);
            }
        });
        context.close();
    }

}
